package com.example.server.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.server.api.dto.UserDto;
import com.example.server.api.service.UserManager;
import com.example.server.entity.User;
import com.example.server.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserManagerImpl implements UserManager {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String hello(String name) {
        return "输入的名字:" + name;
    }

    @Override
    public int insertUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        return userMapper.insert(user);
    }

    @Override
    public UserDto getUserByID(Integer id) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("id", id);
        User user = userMapper.selectOne(qw);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        return userDto;
    }

    @Override
    public List<UserDto> listUser(UserDto userDto) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(userDto.getName() != null, User::getName, userDto.getName());
        queryWrapper.eq(userDto.getAge() != null, User::getAge, userDto.getAge());
        return null;
    }

}
